package com.hw.controller;

import com.github.pagehelper.PageInfo;
import com.hw.dto.R;
import com.hw.pojo.Message;
import com.hw.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: MessageController
 * @Description: 实现接口
 * @Author: vie0134
 * @Date: 2020-12-11 10:22
 * @Version: 1.0
 **/
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/addMessage")
    public R addMessage(@RequestBody Message message) {
        messageService.addMessage(message);
        return R.setSuccess(null);
    }

    @RequestMapping("/updateMessage")
    public R updateMessage(@RequestBody Message message) {
        messageService.updateMessage(message);
        return R.setSuccess(null);
    }

    @RequestMapping("/deleteMessage/{id}")
    public R deleteMessage(@PathVariable Integer id) {
        messageService.deleteMessage(id);
        return R.setSuccess(null);
    }

    @RequestMapping("/selectMessageById/{id}")
    public R selectMessageById(@PathVariable Integer id) {
        Message message = messageService.selectMessageById(id);
        return R.setSuccess(message);
    }

    @RequestMapping("/selectMessageByUsername/{username}")
    public R selectMessageByUsername(@PathVariable String username) {
        Message message = messageService.selectMessageByUsername(username);
        return R.setSuccess(message);
    }

    @RequestMapping("/login")
    public R login(@RequestBody Map<String, String> usernameAndPasswordMap, HttpServletRequest request) {
        Message message = messageService.login(usernameAndPasswordMap.get("username"), usernameAndPasswordMap.get("password"));
        request.getSession().setAttribute("message", message);
        return R.setSuccess(null);
    }

    @RequestMapping("/findMessage")
    public R findMessage(@RequestParam(defaultValue = "1") int pageNum,
                         @RequestParam(defaultValue = "2") int pageSize) {
        PageInfo<Message> pageInfo = messageService.findMessage(pageNum, pageSize);
        return R.setSuccess(pageInfo);
    }
}
